package com.sanwei.dialer;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.telephony.TelephonyManager;
import android.view.KeyEvent;

import com.android.internal.telephony.ITelephony;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import static android.content.Context.TELEPHONY_SERVICE;

public class AutoAnswerReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (!Intent.ACTION_NEW_OUTGOING_CALL.equals(intent.getAction())) {
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE);
            int callSta = telephonyManager.getCallState();
            switch (callSta) {
                case TelephonyManager.CALL_STATE_RINGING:
                    try {
                        @SuppressLint("PrivateApi")
                        Method method = Class.forName("android.os.ServiceManager")
                                .getMethod("getService", String.class);
                        IBinder binder = (IBinder) method.invoke(null, new Object[]{TELEPHONY_SERVICE});
                        ITelephony telephony = ITelephony.Stub.asInterface(binder);
                        telephony.answerRingingCall();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
//                    ITelephony telephony = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    break;
                case TelephonyManager.CALL_STATE_IDLE:
                    break;
            }
        }else if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Intent startIntent = new Intent();
            startIntent.setClass(context, MainActivity.class);
            context.startActivity(startIntent);
        }
    }

    @SuppressLint("PrivateApi")
    public synchronized void answerCall(Context context) {

        TelephonyManager mTelephonyManager = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE);

        Method getITelephonyMethod = null;
        try {
            getITelephonyMethod = mTelephonyManager.getClass().getDeclaredMethod("getITelephony");
            getITelephonyMethod.setAccessible(true);

            ITelephony iTelephony = (ITelephony) getITelephonyMethod.invoke(mTelephonyManager);

            iTelephony.answerRingingCall();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }


    }

    private void anserCall(Context context) {
        Intent mintent = new Intent(Intent.ACTION_MEDIA_BUTTON);

        KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK);

        mintent.putExtra("android.intent.extra.KEY_EVENT", keyEvent);

        context.sendOrderedBroadcast(mintent, "android.permission.CALL_PRIVILEGED");

        mintent = new Intent(Intent.ACTION_MEDIA_BUTTON);

        keyEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK);

        mintent.putExtra("android.intent.extra.KEY_EVENT", keyEvent);

        context.sendOrderedBroadcast(mintent, "android.permission.CALL_PRIVILEGED");
    }

}
